home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / rpc_pipes / ntclientlsa.c.z / ntclientlsa.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  10.0 KB  |  387 lines

  1.  
  2. /* 
  3.  *  Unix SMB/Netbios implementation.
  4.  *  Version 1.9.
  5.  *  RPC Pipe client / server routines
  6.  *  Copyright (C) Andrew Tridgell              1992-1997,
  7.  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
  8.  *  Copyright (C) Paul Ashton                       1997.
  9.  *  
  10.  *  This program is free software; you can redistribute it and/or modify
  11.  *  it under the terms of the GNU General Public License as published by
  12.  *  the Free Software Foundation; either version 2 of the License, or
  13.  *  (at your option) any later version.
  14.  *  
  15.  *  This program is distributed in the hope that it will be useful,
  16.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *  GNU General Public License for more details.
  19.  *  
  20.  *  You should have received a copy of the GNU General Public License
  21.  *  along with this program; if not, write to the Free Software
  22.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25.  
  26. #ifdef SYSLOG
  27. #undef SYSLOG
  28. #endif
  29.  
  30. #include "../includes.h"
  31.  
  32. extern int DEBUGLEVEL;
  33. extern pstring username;
  34. extern pstring workgroup;
  35.  
  36. #define CLIENT_TIMEOUT (30*1000)
  37.  
  38. #ifdef NTDOMAIN
  39.  
  40. /****************************************************************************
  41. do a LSA Open Policy
  42. ****************************************************************************/
  43. BOOL do_lsa_open_policy(uint16 fnum, uint32 call_id,
  44.             char *server_name, LSA_POL_HND *hnd)
  45. {
  46.     char *rparam = NULL;
  47.     char *rdata = NULL;
  48.     char *p;
  49.     int rdrcnt,rprcnt;
  50.     pstring data; /* only 1024 bytes */
  51.     uint16 setup[2]; /* only need 2 uint16 setup parameters */
  52.     LSA_Q_OPEN_POL q_o;
  53.     BOOL valid_pol = False;
  54.  
  55.     if (hnd == NULL) return False;
  56.  
  57.     /* create and send a MSRPC command with api LSA_OPENPOLICY */
  58.  
  59.     DEBUG(4,("LSA Open Policy\n"));
  60.  
  61.     /* store the parameters */
  62.     make_q_open_pol(&q_o, server_name, 0, 0, 0x1);
  63.  
  64.     /* turn parameters into data stream */
  65.     p = lsa_io_q_open_pol(False, &q_o, data + 0x18, data, 4, 0);
  66.  
  67.     /* create the request RPC_HDR_RR with no data */
  68.     create_rpc_request(call_id, LSA_OPENPOLICY, data, PTR_DIFF(p, data));
  69.  
  70.     /* create setup parameters. */
  71.     setup[0] = 0x0026; /* 0x26 indicates "transact named pipe" */
  72.     setup[1] = fnum; /* file handle, from the SMBcreateX pipe, earlier */
  73.  
  74.     /* send the data on \PIPE\ */
  75.     if (cli_call_api("\\PIPE\\", 0,
  76.                 0, PTR_DIFF(p, data), 2,
  77.                 1024, BUFFER_SIZE,
  78.                 &rprcnt, &rdrcnt,
  79.                 NULL, data, setup,
  80.                 &rparam, &rdata))
  81.     {
  82.         LSA_R_OPEN_POL r_o;
  83.         RPC_HDR_RR hdr;
  84.         int hdr_len;
  85.         int pkt_len;
  86.  
  87.         DEBUG(5, ("cli_call_api: return OK\n"));
  88.  
  89.         p = rdata;
  90.  
  91.         if (p) p = smb_io_rpc_hdr_rr   (True, &hdr, p, rdata, 4, 0);
  92.         if (p) p = align_offset(p, rdata, 4); /* oh, what a surprise */
  93.  
  94.         hdr_len = PTR_DIFF(p, rdata);
  95.  
  96.         if (p && hdr_len != hdr.hdr.frag_len - hdr.alloc_hint)
  97.         {
  98.             /* header length not same as calculated header length */
  99.             DEBUG(2,("do_lsa_open_policy: hdr_len %x != frag_len-alloc_hint %x\n",
  100.                       hdr_len, hdr.hdr.frag_len - hdr.alloc_hint));
  101.             p = NULL;
  102.         }
  103.  
  104.         if (p) p = lsa_io_r_open_pol(True, &r_o, p, rdata, 4, 0);
  105.         
  106.         pkt_len = PTR_DIFF(p, rdata);
  107.  
  108.         if (p && pkt_len != hdr.hdr.frag_len)
  109.         {
  110.             /* packet data size not same as reported fragment length */
  111.             DEBUG(2,("do_lsa_open_policy: pkt_len %x != frag_len \n",
  112.                                        pkt_len, hdr.hdr.frag_len));
  113.             p = NULL;
  114.         }
  115.  
  116.         if (p && r_o.status != 0)
  117.         {
  118.             /* report error code */
  119.             DEBUG(0,("LSA_OPENPOLICY: nt_status error %lx\n", r_o.status));
  120.             p = NULL;
  121.         }
  122.  
  123.         if (p)
  124.         {
  125.             /* ok, at last: we're happy. return the policy handle */
  126.             memcpy(hnd, r_o.pol.data, sizeof(hnd->data));
  127.             valid_pol = True;
  128.         }
  129.     }
  130.  
  131.     if (rparam) free(rparam);
  132.     if (rdata) free(rdata);
  133.  
  134.     return valid_pol;
  135. }
  136.  
  137. /****************************************************************************
  138. do a LSA Query Info Policy
  139. ****************************************************************************/
  140. BOOL do_lsa_query_info_pol(uint16 fnum, uint32 call_id,
  141.             LSA_POL_HND *hnd, uint16 info_class,
  142.             fstring domain_name, pstring domain_sid)
  143. {
  144.     char *rparam = NULL;
  145.     char *rdata = NULL;
  146.     char *p;
  147.     int rdrcnt,rprcnt;
  148.     pstring data; /* only 1024 bytes */
  149.     uint16 setup[2]; /* only need 2 uint16 setup parameters */
  150.     LSA_Q_QUERY_INFO q_q;
  151.     BOOL valid_response = False;
  152.  
  153.     if (hnd == NULL || domain_name == NULL || domain_sid == NULL) return False;
  154.  
  155.     /* create and send a MSRPC command with api LSA_QUERYINFOPOLICY */
  156.  
  157.     DEBUG(4,("LSA Query Info Policy\n"));
  158.  
  159.     /* store the parameters */
  160.     make_q_query(&q_q, hnd, info_class);
  161.  
  162.     /* turn parameters into data stream */
  163.     p = lsa_io_q_query(False, &q_q, data + 0x18, data, 4, 0);
  164.  
  165.     /* create the request RPC_HDR_RR with no data */
  166.     create_rpc_request(call_id, LSA_QUERYINFOPOLICY, data, PTR_DIFF(p, data));
  167.  
  168.     /* create setup parameters. */
  169.     setup[0] = 0x0026; /* 0x26 indicates "transact named pipe" */
  170.     setup[1] = fnum; /* file handle, from the SMBcreateX pipe, earlier */
  171.  
  172.     /* send the data on \PIPE\ */
  173.     if (cli_call_api("\\PIPE\\", 0,
  174.                 0, PTR_DIFF(p, data), 2,
  175.                 1024, BUFFER_SIZE,
  176.                 &rprcnt, &rdrcnt,
  177.                 NULL, data, setup,
  178.                 &rparam, &rdata))
  179.     {
  180.         LSA_R_QUERY_INFO r_q;
  181.         RPC_HDR_RR hdr;
  182.         int hdr_len;
  183.         int pkt_len;
  184.  
  185.         DEBUG(5, ("cli_call_api: return OK\n"));
  186.  
  187.         p = rdata;
  188.  
  189.         if (p) p = smb_io_rpc_hdr_rr   (True, &hdr, p, rdata, 4, 0);
  190.         if (p) p = align_offset(p, rdata, 4); /* oh, what a surprise */
  191.  
  192.         hdr_len = PTR_DIFF(p, rdata);
  193.  
  194.         if (p && hdr_len != hdr.hdr.frag_len - hdr.alloc_hint)
  195.         {
  196.             /* header length not same as calculated header length */
  197.             DEBUG(2,("do_lsa_query_info: hdr_len %x != frag_len-alloc_hint %x\n",
  198.                       hdr_len, hdr.hdr.frag_len - hdr.alloc_hint));
  199.             p = NULL;
  200.         }
  201.  
  202.         if (p) p = lsa_io_r_query(True, &r_q, p, rdata, 4, 0);
  203.         
  204.         pkt_len = PTR_DIFF(p, rdata);
  205.  
  206.         if (p && pkt_len != hdr.hdr.frag_len)
  207.         {
  208.             /* packet data size not same as reported fragment length */
  209.             DEBUG(2,("do_lsa_query_info: pkt_len %x != frag_len \n",
  210.                                        pkt_len, hdr.hdr.frag_len));
  211.             p = NULL;
  212.         }
  213.  
  214.         if (p && r_q.status != 0)
  215.         {
  216.             /* report error code */
  217.             DEBUG(0,("LSA_QUERYINFOPOLICY: nt_status error %lx\n", r_q.status));
  218.             p = NULL;
  219.         }
  220.  
  221.         if (p && r_q.info_class != q_q.info_class)
  222.         {
  223.             /* report different info classes */
  224.             DEBUG(0,("LSA_QUERYINFOPOLICY: error info_class (q,r) differ - (%x,%x)\n",
  225.                     q_q.info_class, r_q.info_class));
  226.             p = NULL;
  227.         }
  228.  
  229.         if (p)
  230.         {
  231.             /* ok, at last: we're happy. */
  232.             switch (r_q.info_class)
  233.             {
  234.                 case 3:
  235.                 {
  236.                     char *dom_name = unistrn2(r_q.dom.id3.uni_domain_name.buffer,
  237.                                               r_q.dom.id3.uni_domain_name.uni_str_len);
  238.                     char *dom_sid  = dom_sid_to_string(&(r_q.dom.id3.dom_sid));
  239.                     fstrcpy(domain_name, dom_name);
  240.                     pstrcpy(domain_sid , dom_sid);
  241.  
  242.                     valid_response = True;
  243.                     break;
  244.                 }
  245.                 case 5:
  246.                 {
  247.                     char *dom_name = unistrn2(r_q.dom.id5.uni_domain_name.buffer,
  248.                                               r_q.dom.id5.uni_domain_name.uni_str_len);
  249.                     char *dom_sid  = dom_sid_to_string(&(r_q.dom.id5.dom_sid));
  250.                     fstrcpy(domain_name, dom_name);
  251.                     pstrcpy(domain_sid , dom_sid);
  252.  
  253.                     valid_response = True;
  254.                     break;
  255.                 }
  256.                 default:
  257.                 {
  258.                     DEBUG(3,("LSA_QUERYINFOPOLICY: unknown info class\n"));
  259.                     domain_name[0] = 0;
  260.                     domain_sid [0] = 0;
  261.  
  262.                     break;
  263.                 }
  264.             }
  265.             DEBUG(3,("LSA_QUERYINFOPOLICY (level %x): domain:%s  domain sid:%s\n",
  266.                       r_q.info_class, domain_name, domain_sid));
  267.         }
  268.     }
  269.  
  270.     if (rparam) free(rparam);
  271.     if (rdata) free(rdata);
  272.  
  273.     return valid_response;
  274. }
  275.  
  276. /****************************************************************************
  277. do a LSA Close
  278. ****************************************************************************/
  279. BOOL do_lsa_close(uint16 fnum, uint32 call_id,
  280.             LSA_POL_HND *hnd)
  281. {
  282.     char *rparam = NULL;
  283.     char *rdata = NULL;
  284.     char *p;
  285.     int rdrcnt,rprcnt;
  286.     pstring data; /* only 1024 bytes */
  287.     uint16 setup[2]; /* only need 2 uint16 setup parameters */
  288.     LSA_Q_CLOSE q_c;
  289.     BOOL valid_close = False;
  290.  
  291.     if (hnd == NULL) return False;
  292.  
  293.     /* create and send a MSRPC command with api LSA_OPENPOLICY */
  294.  
  295.     DEBUG(4,("LSA Close\n"));
  296.  
  297.     /* store the parameters */
  298.     make_q_close(&q_c, hnd);
  299.  
  300.     /* turn parameters into data stream */
  301.     p = lsa_io_q_close(False, &q_c, data + 0x18, data, 4, 0);
  302.  
  303.     /* create the request RPC_HDR_RR with no data */
  304.     create_rpc_request(call_id, LSA_CLOSE, data, PTR_DIFF(p, data));
  305.  
  306.     /* create setup parameters. */
  307.     setup[0] = 0x0026; /* 0x26 indicates "transact named pipe" */
  308.     setup[1] = fnum; /* file handle, from the SMBcreateX pipe, earlier */
  309.  
  310.     /* send the data on \PIPE\ */
  311.     if (cli_call_api("\\PIPE\\", 0,
  312.                 0, PTR_DIFF(p, data), 2,
  313.                 1024, BUFFER_SIZE,
  314.                 &rprcnt, &rdrcnt,
  315.                 NULL, data, setup,
  316.                 &rparam, &rdata))
  317.     {
  318.         LSA_R_CLOSE r_c;
  319.         RPC_HDR_RR hdr;
  320.         int hdr_len;
  321.         int pkt_len;
  322.  
  323.         DEBUG(5, ("cli_call_api: return OK\n"));
  324.  
  325.         p = rdata;
  326.  
  327.         if (p) p = smb_io_rpc_hdr_rr   (True, &hdr, p, rdata, 4, 0);
  328.         if (p) p = align_offset(p, rdata, 4); /* oh, what a surprise */
  329.  
  330.         hdr_len = PTR_DIFF(p, rdata);
  331.  
  332.         if (p && hdr_len != hdr.hdr.frag_len - hdr.alloc_hint)
  333.         {
  334.             /* header length not same as calculated header length */
  335.             DEBUG(2,("do_lsa_close: hdr_len %x != frag_len-alloc_hint %x\n",
  336.                       hdr_len, hdr.hdr.frag_len - hdr.alloc_hint));
  337.             p = NULL;
  338.         }
  339.  
  340.         if (p) p = lsa_io_r_close(True, &r_c, p, rdata, 4, 0);
  341.         
  342.         pkt_len = PTR_DIFF(p, rdata);
  343.  
  344.         if (p && pkt_len != hdr.hdr.frag_len)
  345.         {
  346.             /* packet data size not same as reported fragment length */
  347.             DEBUG(2,("do_lsa_close: pkt_len %x != frag_len \n",
  348.                                        pkt_len, hdr.hdr.frag_len));
  349.             p = NULL;
  350.         }
  351.  
  352.         if (p && r_c.status != 0)
  353.         {
  354.             /* report error code */
  355.             DEBUG(0,("LSA_OPENPOLICY: nt_status error %lx\n", r_c.status));
  356.             p = NULL;
  357.         }
  358.  
  359.         if (p)
  360.         {
  361.             /* check that the returned policy handle is all zeros */
  362.             int i;
  363.             valid_close = True;
  364.  
  365.             for (i = 0; i < sizeof(r_c.pol.data); i++)
  366.             {
  367.                 if (r_c.pol.data[i] != 0)
  368.                 {
  369.                     valid_close = False;
  370.                     break;
  371.                 }
  372.             }    
  373.             if (!valid_close)
  374.             {
  375.                 DEBUG(0,("LSA_CLOSE: non-zero handle returned\n"));
  376.             }
  377.         }
  378.     }
  379.  
  380.     if (rparam) free(rparam);
  381.     if (rdata) free(rdata);
  382.  
  383.     return valid_close;
  384. }
  385.  
  386. #endif /* NTDOMAIN */
  387.